﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    //A::= B | C
    //-> A::= M 
    //   M::= B 
    //   M::= C
    public class EbnfAlternate : EbnfNode
    {
        EbnfNode first;
        EbnfNode rest;

        public EbnfAlternate(EbnfNode first, EbnfNode rest)
        {
            this.first = first;
            this.rest = rest;
        }

        static int cnt = 1;
        public override EbnfNode Expand(Stack<EbnfAssign> Open)
        {
            first = first.Expand(Open);
            rest = rest.Expand(Open);

            EbnfMeta head = new EbnfMeta(string.Format("A{0}", cnt++));
            EbnfAssign ass1 = new EbnfAssign(head, first);
            EbnfAssign ass2 = new EbnfAssign(head, rest);

            Open.Push(ass1);
            Open.Push(ass2);

            return head;
        }

        public override string ToString()
        {
            return first.ToString() + "|" + rest.ToString();
        }
    }
}
